#!/usr/bin/python
# -*- coding:utf-8 -*-
# @time: 2021/5/13 5:58 下午

import os
import base64

from wass.settings import LANGUAGE_CODE as DJANGO_LANGUAGE_CODE, SOURCE_FOLDER
from utilities.enums import LogModuleName, LogTypeName, SEVERITY_MAP, TaskType, SecurityEventPlugins, LANGUAGE_MAP


LANGUAGE_CODE = LANGUAGE_MAP[DJANGO_LANGUAGE_CODE]

# HTTP请求方法
HTTP_METHOD = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'TRACE']

# 日志中心前端展示配置
# 日志登录结果中文名
LOGIN_RET_DICT = {
    'success': '成功',
    'fail': '失败',
}

# 日志登录动作中文名
LOGIN_ACTION_DICT = {'login': '登录', 'logout': '退出', 'password': '重置密码'}
# 登录操作的模块名
LOGIN_MODULE = "login"

# 操作日志模块中文名
MODULE_NAME_DICT = {
    LogModuleName.monitor_list.value: '监测列表',
    LogModuleName.task_management.value: '任务管理',
    LogModuleName.vuln_db.value: '漏洞库',
    LogModuleName.warn_center.value: '告警中心',
    LogModuleName.log_center.value: '日志中心',
    LogModuleName.policy_management.value: '策略管理',
    LogModuleName.net_management.value: '网络配置',
    LogModuleName.system_management.value: '系统管理',
    LogModuleName.user_management.value: '用户管理',
    LogModuleName.backup.value: '备份恢复',
}

# 日志子模块中文名
SUBMODULE_NAME_DICT = {
    LogTypeName.task.value: '任务管理',
    LogTypeName.target_group.value: '任务组',
    LogTypeName.target.value: '业务系统管理',
    LogTypeName.log_management.value: '日志管理',
    LogTypeName.profile_template.value: '漏洞模板设置',
    LogTypeName.keyword.value: '敏感词设置',
    LogTypeName.change_check_config.value: '变更基准设置',
    # 'ports': '端口配置',
    'ssl': '证书管理',
    'other': "其他模块",  # 告警设置
    'export_log': '导出日志',
    LogTypeName.upload_ssl.value: "产品证书上传",
    LogTypeName.system_upgrade.value: "系统升级",
    # 'data_upgrade': "数据升级",  # 需求暂去除数据升级子模块项
    LogTypeName.system_network.value: "网口配置",
    "system_dns": "DNS配置",
    LogTypeName.remote_assistance.value: "远程协助",
    LogTypeName.turn_off_device.value: "关闭设备",
    LogTypeName.restart_device.value: "重启设备",
    LogTypeName.ntp.value: "系统时间",
    LogTypeName.route.value: "路由配置",
    LogTypeName.snmp.value: "SNMP配置",
    LogTypeName.backup.value: "备份恢复",
    LogTypeName.account.value: "账号管理",
    LogTypeName.permission.value: "权限管理",
    LogTypeName.user_group.value: "角色管理",
    LogTypeName.warn_center.value: "告警中心",
    LogTypeName.api_token.value: "授权token",
    LogTypeName.api_url.value: "推送URL",
    LogTypeName.ssh.value: "SSH配置",
}

# 模块子模块包含关系
MODULE_SUBMODULE_CONTAINS = {
    LogModuleName.monitor_list.value: [LogTypeName.target.value],
    LogModuleName.task_management.value: [
        LogTypeName.task.value,
        LogTypeName.target_group.value,
    ],
    LogModuleName.vuln_db.value: [],
    LogModuleName.warn_center.value: [],
    LogModuleName.log_center.value: [
        LogTypeName.log_management.value,
        LogTypeName.login.value,
    ],
    LogModuleName.policy_management.value: [
        LogTypeName.profile_template.value,
        LogTypeName.keyword.value,
        LogTypeName.change_check_config.value
    ],
    LogModuleName.net_management.value: [
        LogTypeName.system_network.value,
        LogTypeName.route.value,
        LogTypeName.snmp.value,
        LogTypeName.system_dns.value,
    ],
    LogModuleName.system_management.value: [
        LogTypeName.ntp.value,
        LogTypeName.system_upgrade.value,
        LogTypeName.upload_ssl.value,
        LogTypeName.remote_assistance.value,
        LogTypeName.turn_off_device.value,
        LogTypeName.restart_device.value
    ],
    LogModuleName.backup.value: [],
    'account_manager': [
        'account',
        'permission',
        'user_group',
        LogTypeName.api_token.value,
        LogTypeName.api_url.value
    ],
}

# 日志操作中文名
ACTION_NAME_DICT = {'add': '增加', 'up': '更新', 'del': '删除'}

MODE_DESC = {"proxy": "反向代理模式", "transparent": "透明直连模式", "offline": "旁路模式"}


IPACTIONS = {'ban': '主动拦截', 'js': '指纹监控', 'ai': 'AI识别', 'captcha': '验证码', '': '关闭'}

IPACTIONS_PAGE_CONF = [
    {
        'tab': '重点行业攻击清洗',
        'ipactions': [
            'gd_gov',
            'gd_finance',
            'gd_edu',
            'gd_news',
            'gd_medical',
            'gd_other',
        ],
    },
    {'tab': '网络机器流量清洗', 'ipactions': ['proxy', 'idc', 'onion']},
    {'tab': '特定攻击行为清洗', 'ipactions': ['scaner', 'brute_force']},
    {'tab': '重点保障攻击防护', 'ipactions': ['gd_cyber_protection']},
    {
        'tab': 'IDC访问控制',
        'ipactions': [
            'gd_idc_aliyun',
            'gd_idc_tencentyun',
            'gd_idc_baiduyun',
            'gd_idc_ucloud',
            'gd_idc_amazon',
            'gd_idc_microsoft',
            'gd_idc_google',
            'gd_idc_other',
        ],
    },
]

IPACTIONS_TABLES = {}

SecurityEventPlugins_NAME_MAP = {
    'zh_cn': {
        SecurityEventPlugins.malscan.value: '挂马',
        SecurityEventPlugins.keyword.value: '敏感词',
        SecurityEventPlugins.seo_hijack.value: 'seo劫持',
        SecurityEventPlugins.black_links.value: '暗链',
        SecurityEventPlugins.privacy_disclosure.value: '隐私信息泄漏',
        SecurityEventPlugins.cryptojacking.value: '挖矿',
        SecurityEventPlugins.broken_links.value: '坏链',
    }
}

SEVERITY_NAME_MAP = {
    'zh_cn': {
        SEVERITY_MAP[0]: '未知',
        SEVERITY_MAP[1]: '信息',
        SEVERITY_MAP[2]: '低危',
        SEVERITY_MAP[3]: '中危',
        SEVERITY_MAP[4]: '高危',
        SEVERITY_MAP[5]: '严重',
    }
}
# 数据库里存的是数字
SEVERITY2_NAME_MAP = {
    'zh_cn': {
        0: '未知',
        1: '信息',
        2: '低危',
        3: '中危',
        4: '高危',
        5: '严重',
    }
}

CHANGE_CHECK_TYPE_MAP = {
    'text': '内容',
    'structure': '结构',
    'resource': '资源',
}

TaskType_NAME_MAP = {
    'zh_cn': {
        TaskType.securityEvent.value: {
            'name': '安全事件',
            'map': SecurityEventPlugins_NAME_MAP,
        },
        TaskType.vul.value: {'name': '漏洞', 'map': SEVERITY_NAME_MAP},
        TaskType.availability.value: {'name': '可用性', 'map': None},
        TaskType.risk_link.value: {'name': '风险外链', 'map': SEVERITY_NAME_MAP},
        TaskType.change_check.value: {'name': '篡改', 'map': SEVERITY_NAME_MAP},
    }
}


for table in IPACTIONS_PAGE_CONF:
    for item in table["ipactions"]:
        IPACTIONS_TABLES[item] = table["tab"]


availabilityMap_NAME_MAP = {
    'zh_cn': {
        'dns': '域名解析',
        'http_get': 'HTTP响应',
        'ping': 'Ping响应',
        'http_get_full_time': '页面读取'}
}

CrawlerScope_NAME_MAP = {
    'zh_cn': {2: '顶级域名', 3: '自定义域', 4: '严格域', 5: '当前目录'}
}

Modules_NAME_MAP = {
    'zh_cn': {'target_info': '站点信息', 'availability': '可用性', 'securityEvent': '安全事件', 'risk_link': '风险外链',
              'vul': '漏洞'}
}

JOBCONFIG_NAME_MAP = {
    'zh_cn': {
        'crawler_max_page': '爬取最大URL',
        'crawler_depth': '爬取深度',
    }
}


class Picture(object):
    allow_file = ['.png', '.svg', '.jpg', '.jpeg']

    def get_file(self, filename):
        filepath = ""
        for _itm in self.allow_file:
            _tmp_path = os.path.join(SOURCE_FOLDER, f"{filename}{_itm}")
            if os.path.isfile(_tmp_path):
                filepath = _tmp_path

        if not filepath:
            return None

        return open(filepath, 'rb')

    def get_base64(self, filename):
        return base64.b64encode(self.get_file(filename).read()).decode()
